package com.seanchenxi.gwt.wordpress.xmlrpc.service.impl;

import java.util.ArrayList;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.seanchenxi.gwt.wordpress.domain.model.Author;
import com.seanchenxi.gwt.wordpress.domain.model.Blog;
import com.seanchenxi.gwt.wordpress.domain.model.Category;
import com.seanchenxi.gwt.wordpress.domain.model.CategoryCreator;
import com.seanchenxi.gwt.wordpress.domain.model.Comment;
import com.seanchenxi.gwt.wordpress.domain.model.CommentCount;
import com.seanchenxi.gwt.wordpress.domain.model.CommentCreator;
import com.seanchenxi.gwt.wordpress.domain.model.CommentEditor;
import com.seanchenxi.gwt.wordpress.domain.model.CommentFinder;
import com.seanchenxi.gwt.wordpress.domain.model.FileData;
import com.seanchenxi.gwt.wordpress.domain.model.Options;
import com.seanchenxi.gwt.wordpress.domain.model.Page;
import com.seanchenxi.gwt.wordpress.domain.model.PageEditor;
import com.seanchenxi.gwt.wordpress.domain.model.PageList;
import com.seanchenxi.gwt.wordpress.domain.model.PageTemplate;
import com.seanchenxi.gwt.wordpress.domain.model.SimpleCategory;
import com.seanchenxi.gwt.wordpress.domain.model.Tag;
import com.seanchenxi.gwt.wordpress.domain.model.WpFile;
import com.seanchenxi.gwt.wordpress.xmlrpc.service.WordPressServiceAsync;
import com.seanchenxi.gwt.wordpress.xmlrpc.service.constant.HasWordPressServiceMethodNames;
import com.seanchenxi.gwt.wordpress.xmlrpc.service.model.handler.AuthorsResponseHandler;
import com.seanchenxi.gwt.wordpress.xmlrpc.service.model.handler.BlogsResponseHandler;
import com.seanchenxi.gwt.wordpress.xmlrpc.service.model.handler.CategoriesResponseHandler;
import com.seanchenxi.gwt.wordpress.xmlrpc.service.model.handler.CommentCountsResponseHandler;
import com.seanchenxi.gwt.wordpress.xmlrpc.service.model.handler.CommentResponseHandler;
import com.seanchenxi.gwt.wordpress.xmlrpc.service.model.handler.CommentsResponseHandler;
import com.seanchenxi.gwt.wordpress.xmlrpc.service.model.handler.PageListsResponseHandler;
import com.seanchenxi.gwt.wordpress.xmlrpc.service.model.handler.PageResponseHandler;
import com.seanchenxi.gwt.wordpress.xmlrpc.service.model.handler.PagesResponseHandler;
import com.seanchenxi.gwt.wordpress.xmlrpc.service.model.handler.StatusResponseHandler;
import com.seanchenxi.gwt.wordpress.xmlrpc.service.model.handler.TagsResponseHandler;
import com.seanchenxi.gwt.xmlrpc.client.XmlRpcClient;
import com.seanchenxi.gwt.xmlrpc.client.XmlRpcRequest;

/**
 * The Class WordPressServiceAsyncImpl.
 * 
 * @author Xi
 */
public class WordPressServiceAsyncImpl extends XmlRpcClient implements
		WordPressServiceAsync, HasWordPressServiceMethodNames {

	/**
	 * Instantiates a new word press asynchronous service implementation.
	 * 
	 * @param url
	 *            the xmlrpc's url
	 */
	public WordPressServiceAsyncImpl(String url) {
		super(url);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.seanchenxi.gwt.wordpress.service.WordPressServiceAsync#getUsersBlogs
	 * (java.lang.String, java.lang.String,
	 * com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void getUsersBlogs(String username, String password,
			final AsyncCallback<ArrayList<Blog>> callback) {

		XmlRpcRequest<ArrayList<Blog>> xmlRpcRequest = createRequest(
				GET_USERS_BLOGS, username, password);

		send(xmlRpcRequest, callback, new BlogsResponseHandler());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.seanchenxi.gwt.wordpress.service.WordPressServiceAsync#getPage(int,
	 * int, java.lang.String, java.lang.String,
	 * com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void getPage(int blogId, int pageId, String username,
			String password, final AsyncCallback<Page> callback) {

		XmlRpcRequest<Page> xmlRpcRequest = createRequest(GET_PAGE, blogId,
				pageId, username, password);

		send(xmlRpcRequest, callback, new PageResponseHandler());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.seanchenxi.gwt.wordpress.service.WordPressServiceAsync#getPages(int,
	 * java.lang.String, java.lang.String,
	 * com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void getPages(int blogId, String username, String password,
			final AsyncCallback<ArrayList<Page>> callback) {

		XmlRpcRequest<ArrayList<Page>> xmlRpcRequest = createRequest(GET_PAGES,
				blogId, username, password);

		send(xmlRpcRequest, callback, new PagesResponseHandler());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.seanchenxi.gwt.wordpress.service.WordPressServiceAsync#newPage(int,
	 * java.lang.String, java.lang.String,
	 * com.seanchenxi.gwt.wordpress.domain.model.PageEditor, boolean,
	 * com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void newPage(int blogId, String username, String password,
			PageEditor content, boolean publish, AsyncCallback<Integer> callback) {

		XmlRpcRequest<Integer> xmlRpcRequest = createRequest(NEW_PAGE, blogId,
				username, password, content);

		send(xmlRpcRequest, callback);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.seanchenxi.gwt.wordpress.service.WordPressServiceAsync#deletePage
	 * (int, java.lang.String, java.lang.String, int,
	 * com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void deletePage(int blogId, String username, String password,
			int pageId, AsyncCallback<Boolean> callback) {

		XmlRpcRequest<Boolean> xmlRpcRequest = createRequest(DELETE_PAGE,
				blogId, username, password, pageId);

		send(xmlRpcRequest, callback);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.seanchenxi.gwt.wordpress.service.WordPressServiceAsync#editPage(int,
	 * int, java.lang.String, java.lang.String,
	 * com.seanchenxi.gwt.wordpress.domain.model.PageEditor, boolean,
	 * com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void editPage(int blogId, int pageId, String username,
			String password, PageEditor content, boolean publish,
			AsyncCallback<Boolean> callback) {

		XmlRpcRequest<Boolean> xmlRpcRequest = createRequest(EDIT_PAGE, blogId,
				username, password, content, publish);

		send(xmlRpcRequest, callback);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.seanchenxi.gwt.wordpress.service.WordPressServiceAsync#getPageList
	 * (int, java.lang.String, java.lang.String,
	 * com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void getPageList(int blogId, String username, String password,
			AsyncCallback<ArrayList<PageList>> callback) {

		XmlRpcRequest<ArrayList<PageList>> xmlRpcRequest = createRequest(
				GET_PAGELIST, blogId, username, password);

		send(xmlRpcRequest, callback, new PageListsResponseHandler());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.seanchenxi.gwt.wordpress.service.WordPressServiceAsync#getAuthors
	 * (int, java.lang.String, java.lang.String,
	 * com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void getAuthors(int blogId, String username, String password,
			AsyncCallback<ArrayList<Author>> callback) {

		XmlRpcRequest<ArrayList<Author>> xmlRpcRequest = createRequest(
				GET_AUTHORS, blogId, username, password);

		send(xmlRpcRequest, callback, new AuthorsResponseHandler());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.seanchenxi.gwt.wordpress.service.WordPressServiceAsync#getCategories
	 * (int, java.lang.String, java.lang.String,
	 * com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void getCategories(int blogId, String username, String password,
			AsyncCallback<ArrayList<Category>> callback) {

		XmlRpcRequest<ArrayList<Category>> xmlRpcRequest = createRequest(
				GET_CATEGORIES, blogId, username, password);

		send(xmlRpcRequest, callback, new CategoriesResponseHandler<Category>());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.seanchenxi.gwt.wordpress.service.WordPressServiceAsync#getTags(int,
	 * java.lang.String, java.lang.String,
	 * com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void getTags(int blogId, String username, String password,
			AsyncCallback<ArrayList<Tag>> callback) {

		XmlRpcRequest<ArrayList<Tag>> xmlRpcRequest = createRequest(GET_TAGS,
				blogId, username, password);

		send(xmlRpcRequest, callback, new TagsResponseHandler());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.seanchenxi.gwt.wordpress.service.WordPressServiceAsync#newCategory
	 * (int, java.lang.String, java.lang.String,
	 * com.seanchenxi.gwt.wordpress.domain.model.CategoryCreator,
	 * com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void newCategory(int blogId, String username, String password,
			CategoryCreator categoryCreator, AsyncCallback<Integer> callback) {

		XmlRpcRequest<Integer> xmlRpcRequest = createRequest(NEW_CATEGORY,
				blogId, username, password, categoryCreator);

		send(xmlRpcRequest, callback);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.seanchenxi.gwt.wordpress.service.WordPressServiceAsync#deleteCategory
	 * (int, java.lang.String, java.lang.String, int,
	 * com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void deleteCategory(int blogId, String username, String password,
			int categoryId, AsyncCallback<Boolean> callback) {

		XmlRpcRequest<Boolean> xmlRpcRequest = createRequest(DELETE_CATEGORY,
				blogId, username, password, categoryId);

		send(xmlRpcRequest, callback);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.seanchenxi.gwt.wordpress.service.WordPressServiceAsync#suggestCategories
	 * (int, java.lang.String, java.lang.String, java.lang.String, int,
	 * com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void suggestCategories(int blogId, String username, String password,
			String category, int max_results,
			AsyncCallback<ArrayList<SimpleCategory>> callback) {

		XmlRpcRequest<ArrayList<SimpleCategory>> xmlRpcRequest = createRequest(
				SUGGEST_CATEGORIES, blogId, username, password);

		send(xmlRpcRequest, callback,
				new CategoriesResponseHandler<SimpleCategory>());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.seanchenxi.gwt.wordpress.service.WordPressServiceAsync#uploadFile
	 * (int, java.lang.String, java.lang.String,
	 * com.seanchenxi.gwt.wordpress.domain.model.FileData,
	 * com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void uploadFile(int blogId, String username, String password,
			FileData data, AsyncCallback<WpFile> callback) {
		// TODO We do nothing now for this method. Will be done in next version
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.seanchenxi.gwt.wordpress.service.WordPressServiceAsync#getCommentCount
	 * (int, java.lang.String, java.lang.String, java.lang.String,
	 * com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void getCommentCount(int blogId, String username, String password,
			String postId, AsyncCallback<ArrayList<CommentCount>> callback) {

		XmlRpcRequest<ArrayList<CommentCount>> xmlRpcRequest = createRequest(
				GET_COMMENTCOUNT, blogId, username, password);

		send(xmlRpcRequest, callback, new CommentCountsResponseHandler());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.seanchenxi.gwt.wordpress.service.WordPressServiceAsync#getPostStatusList
	 * (int, java.lang.String, java.lang.String,
	 * com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void getPostStatusList(int blogId, String username, String password,
			AsyncCallback<ArrayList<String>> callback) {

		XmlRpcRequest<ArrayList<String>> xmlRpcRequest = createRequest(
				GET_POSTSTATUS_LIST, blogId, username, password);

		send(xmlRpcRequest, callback, new StatusResponseHandler());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.seanchenxi.gwt.wordpress.service.WordPressServiceAsync#getPageStatusList
	 * (int, java.lang.String, java.lang.String,
	 * com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void getPageStatusList(int blogId, String username, String password,
			AsyncCallback<ArrayList<String>> callback) {

		XmlRpcRequest<ArrayList<String>> xmlRpcRequest = createRequest(
				GET_PAGESTATUS_LIST, blogId, username, password);

		send(xmlRpcRequest, callback, new StatusResponseHandler());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.seanchenxi.gwt.wordpress.service.WordPressServiceAsync#getPageTemplates
	 * (int, java.lang.String, java.lang.String,
	 * com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void getPageTemplates(int blogId, String username, String password,
			AsyncCallback<ArrayList<PageTemplate>> callback) {

		XmlRpcRequest<ArrayList<PageTemplate>> xmlRpcRequest = createRequest(
				GET_PAGETEMPLATES, blogId, username, password);

		send(xmlRpcRequest, callback);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.seanchenxi.gwt.wordpress.service.WordPressServiceAsync#getOptions
	 * (int, java.lang.String, java.lang.String, java.util.ArrayList,
	 * com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void getOptions(int blogId, String username, String password,
			ArrayList<String> optionNames,
			AsyncCallback<ArrayList<Options>> callback) {

		XmlRpcRequest<ArrayList<Options>> xmlRpcRequest = createRequest(
				GET_OPTIONS, blogId, username, password, optionNames);

		send(xmlRpcRequest, callback);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.seanchenxi.gwt.wordpress.service.WordPressServiceAsync#setOptions
	 * (int, java.lang.String, java.lang.String, java.util.ArrayList,
	 * com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void setOptions(int blogId, String username, String password,
			ArrayList<Options> options,
			AsyncCallback<ArrayList<Options>> callback) {

		XmlRpcRequest<ArrayList<Options>> xmlRpcRequest = createRequest(
				SET_OPTIONS, blogId, username, password, options);

		send(xmlRpcRequest, callback);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.seanchenxi.gwt.wordpress.service.WordPressServiceAsync#getComment
	 * (int, java.lang.String, java.lang.String, int,
	 * com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void getComment(int blogId, String username, String password,
			int commentId, AsyncCallback<Comment> callback) {

		XmlRpcRequest<Comment> xmlRpcRequest = createRequest(GET_COMMENT,
				blogId, username, password, commentId);

		send(xmlRpcRequest, callback, new CommentResponseHandler());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.seanchenxi.gwt.wordpress.service.WordPressServiceAsync#getComments
	 * (int, java.lang.String, java.lang.String,
	 * com.seanchenxi.gwt.wordpress.domain.model.CommentFinder,
	 * com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void getComments(int blogId, String username, String password,
			CommentFinder commentFinder,
			AsyncCallback<ArrayList<Comment>> callback) {

		XmlRpcRequest<ArrayList<Comment>> xmlRpcRequest = createRequest(
				GET_COMMENTS, blogId, username, password, commentFinder);

		send(xmlRpcRequest, callback, new CommentsResponseHandler());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.seanchenxi.gwt.wordpress.service.WordPressServiceAsync#deleteComment
	 * (int, java.lang.String, java.lang.String, int,
	 * com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void deleteComment(int blogId, String username, String password,
			int commentId, AsyncCallback<Boolean> callback) {

		XmlRpcRequest<Boolean> xmlRpcRequest = createRequest(DELETE_COMMENT,
				blogId, username, password, commentId);

		send(xmlRpcRequest, callback);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.seanchenxi.gwt.wordpress.service.WordPressServiceAsync#editComment
	 * (int, java.lang.String, java.lang.String, int,
	 * com.seanchenxi.gwt.wordpress.domain.model.CommentEditor,
	 * com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void editComment(int blogId, String username, String password,
			int commentId, CommentEditor commentEditor,
			AsyncCallback<Boolean> callback) {

		XmlRpcRequest<Boolean> xmlRpcRequest = createRequest(EDIT_COMMENT,
				blogId, username, password, commentId, commentEditor);
		send(xmlRpcRequest, callback);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.seanchenxi.gwt.wordpress.service.WordPressServiceAsync#newComment
	 * (int, java.lang.String, java.lang.String, int,
	 * com.seanchenxi.gwt.wordpress.domain.model.CommentCreator,
	 * com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void newComment(int blogId, String username, String password,
			int postId, CommentCreator commentCreator,
			AsyncCallback<Integer> callback) {

		XmlRpcRequest<Integer> xmlRpcRequest = createRequest(NEW_COMMENT,
				blogId, username, password, postId, commentCreator);
		send(xmlRpcRequest, callback);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.seanchenxi.gwt.wordpress.service.WordPressServiceAsync#
	 * getCommentStatusList(int, java.lang.String, java.lang.String,
	 * com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void getCommentStatusList(int blogId, String username,
			String password, AsyncCallback<ArrayList<String>> callback) {

		XmlRpcRequest<ArrayList<String>> xmlRpcRequest = createRequest(
				GET_COMMENTSTATUS_LIST, blogId, username, password);

		send(xmlRpcRequest, callback, new StatusResponseHandler());
	}

}
